﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZkSurvey.Common.SurveyMgr;
using ZkSurvey.DAL.SurveyMgr;
using ZkSurvey.DAL;

namespace ZkSurvey.BLL.SurveyMgr
{
    public class SurveyBl
    {
        /// <summary>
        /// 获取所有问卷
        /// </summary>
        /// <returns></returns>
        public List<Survey> GetSurveyList()
        {
            return SurveyDa.GetSurveyList();
        }
        /// <summary>
        /// 根据Id查询问卷
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Survey GetSurvey(int id)
        {
            return SurveyDa.GetSurvey(id);
        }

        /// <summary>
        /// 创建问卷
        /// </summary>
        /// <param name="survey"></param>
        public void CreateSurvey(Survey survey)
        {
            survey.CreateTime = DateTime.Now;
            survey.Code = Guid.NewGuid();
            survey.ValidationCode = Guid.NewGuid().ToString().Substring(0, 5);

            //验证是否符合数据要求
            if (!survey.Validated)
                throw new ApplicationException(survey.ValidationMessage);

            SurveyDa.InsertSurvey(survey);
        }

        /// <summary>
        /// 根据Id删除问卷
        /// </summary>
        /// <param name="id"></param>
        public void DeleteSurvey(int id)
        {
            Survey survey = SurveyDa.GetSurvey(id);
            if (survey.Status == SurveyStatus.Running)
                throw new ApplicationException("问卷调查正在进行，不允许删除!");
            if (survey.Status == SurveyStatus.Completed)
                throw new ApplicationException("问卷调查已经完成，不允许删除!");


            using (TranscationHelper trans = TranscationHelper.GetInstance())
            {
                trans.BeginTrans();
                try
                {
                    List<Question> questions = QuestionDa.GetQuestionList(survey.Id, trans);
                    foreach (Question ques in questions)
                    {
                        List<Option> options = OptionDa.GetOptionList(ques.Id, trans);
                        foreach (Option opt in options)
                        {
                            OptionDa.Delete(opt.Id,trans);
                        }
                        QuestionDa.DeleteQuestion(ques.Id);
                    }
                    SurveyDa.DeleteSurvey(id,trans);
                    
                    trans.CommitTrans();
                }
                catch
                {
                    trans.RollTrans();
                    throw;
                }
            }
        }

        public void UpdateSurvey(Survey survey)
        {
            //验证是否符合数据要求
            if (!survey.Validated)
                throw new ApplicationException(survey.ValidationMessage);

            SurveyDa.UpdateSurvey(survey);
        }

        public Survey GetSurvey(Guid code)
        {
            List<Survey> surveies = SurveyDa.GetSurveyList();
            return surveies.Find(s => s.Code.Equals(code));
        }
    }
}
